program sum1nsq

use sums
use time
implicit none

integer :: i,j,n
double precision exact
real :: result,ptoc
real, dimension(:), allocatable :: r
real, dimension(:), allocatable :: walltime, processortime    
write(unit=*, fmt="(A)", advance="no") "Enter the value of n: "
read *, n

allocate(r(n**2))
r = (/ (real(i), i=n**2,1,-1)/)
print *, 'Allocation array n succesful: ', allocated(r)

exact = 0.5*(n**2)*(n**2+1)

print *, 'Exact : ', exact
result = sum1(r)
print *, 'Sum1 : ', result, 'Relative', (result-exact)/exact 
result = sum2(r,n)
print *, 'Sum2 : ', result, 'Relative', (result-exact)/exact 
result = sum3(r)
print *, 'Sum3 : ', result, 'Relative', (result-exact)/exact 
result = sum4(r)
print *, 'Sum4 : ', result, 'Relative', (result-exact)/exact 
result = sum5(r)
print *, 'Sum5 : ', result, 'Relative', (result-exact)/exact 

allocate(walltime(5))
allocate(processortime(5))

do i=1,5000,100

deallocate(r)
allocate(r(i**2))
r = (/ (real(j), j=i**2,1,-1)/)

call wallclock_tic()
result = sum1(r)
walltime(1) = wallclock_toc()

call processor_tic()
result = sum1(r)
processortime(1) = processor_toc()

call wallclock_tic()
result = sum2(r,i)
walltime(2) = wallclock_toc()

call processor_tic()
result = sum2(r,i)
processortime(2) = processor_toc()

call wallclock_tic()
result = sum3(r)
walltime(3) = wallclock_toc()

call processor_tic()
result = sum3(r)
processortime(3) = processor_toc()

call wallclock_tic()
result = sum4(r)
walltime(4) = wallclock_toc()

call processor_tic()
result = sum4(r)
processortime(4) = processor_toc()

call wallclock_tic()
result = sum5(r)
walltime(5) = wallclock_toc()

call processor_tic()
result = sum5(r)
processortime(5) = processor_toc()

print *, i, walltime, processortime
enddo
deallocate(r)
end program